<script setup lang="ts">
import { reactive, onMounted } from "vue";
import apiBlog from "@/api/blog/blog";
import apiRead from "@/api/blog/read";
import { useRouter } from "vue-router";
import { getOptNameType } from "@/utils/utils";
import moment from "moment";
let state: any = reactive({
    list: [],
    showList: [],
    pageSize: 10,
    currentPage: 1,
});
let router = useRouter();

const navTo = (id: any) => {
    router.push({ path: "/read", query: { id } });
};
let currentChange = (e: any) => {
    let listCopy = JSON.parse(JSON.stringify(state.list));
    let start = (e - 1) * state.pageSize;
    let end = (e - 1) * state.pageSize + parseInt(state.pageSize);
    state.showList = listCopy.slice(start, end);


    document.documentElement.scrollTop = 0;
};
onMounted(async () => {
    const res = await apiBlog.getlist(null);
    state.list = res.data;
    let listCopy = JSON.parse(JSON.stringify(state.list));
    state.showList = listCopy.slice((state.currentPage - 1) * state.pageSize, (state.currentPage - 1) * state.pageSize + state.pageSize);
    apiRead.update({ path: router.currentRoute.value.name });

});
</script>
<template>
    <div class="blog-warp">
        <div class="blog-item" v-for="(item, index) in state.showList" :key="index">
            <div class="blog-item-left" @click="navTo(item._id)">
                <el-image :src="item.coverUrl ? item.coverUrl : 'https://pic.imgdb.cn/item/652f397bc458853aefff7856.jpg'"
                    lazy fit="cover"> </el-image>
            </div>
            <div class="blog-item-right">
                <div class="title" @click="navTo(item._id)">
                    <div class="s1" style="font-size: 22px; cursor: pointer">{{ item.title }}</div>
                </div>
                <div class="tip">
                    <div class="toTop" style="color: #67C23A" v-if="item.top">
                        <el-icon>
                            <Filter />
                        </el-icon>
                        置顶
                    </div>
                    <div class="date">
                        <el-icon>
                            <Calendar />
                        </el-icon>
                        {{ moment(item.createTime).format('YYYY-MM-DD') }}
                    </div>
                    <div class="type">
                        <el-icon>
                            <Coin />
                        </el-icon>
                        {{ getOptNameType("blog_type", item.type) }}
                    </div>
                    
                </div>
                <div class="s3 desc" style="color: var(--c-text-3); line-height: 24px">
                    {{ item.desc }}
                </div>
                <div class="read" @click="navTo(item._id)">阅读原文...</div>
            </div>
        </div>
        <div class="pagination">
            <el-pagination background layout="prev, pager, next,page-size" @current-change="currentChange"
                :page-size="state.pageSize * 1" :total="state.list.length" />
        </div>
    </div>
</template>
<style lang="scss" scoped>
.blog-warp {
    .pagination {
        margin: 20px;
        display: flex;
        justify-content: center;
    }

    .blog-item {
        display: flex;
        overflow: hidden;
        height: 230px;
        padding: 10px;
        border: 1px solid #e6e6e6;
        border-top: none;
        transition: 0.3s;

        &:nth-child(1) {
            border-top: 1px solid #e6e6e6;
        }

        &:hover {}

        .title {
            div {
                color: var(--c-text-2);

                &:hover {
                    color: var(--c-text-1);
                }
            }
        }

        .blog-item-left {
            width: 45%;
            flex-shrink: 0;
            overflow: hidden;

            .el-image {
                width: 100%;
                height: 250px;
                transition: 1s;
            }
        }

        .blog-item-right {
            padding: 10px 25px;
            display: flex;
            width: 100%;
            flex-direction: column;

            .desc {
                flex: 1;
            }

            .read {
                color: var(--c-text-2);
                text-decoration: underline;
                transition: 0.3s;
                cursor: pointer;
                margin-left: auto;
                margin-right: 0;

                &:hover {
                    color: var(--c-text-1);
                }
            }

            .tip {
                display: flex;
                color: var(--c-text-3);
                margin: 5px 0;

                &>div {
                    margin-right: 20px;
                    display: flex;
                    align-items: center;

                    .el-icon {
                        margin-right: 5px;
                    }
                }
            }
        }
    }

    @media (max-width: 768px) {

        //博客左右/上下切换
        .blog-item {
            flex-direction: column;
            height: auto;

            .blog-item-left {
                width: 100%;

                .el-image {
                    height: 230px;
                }
            }
        }

        .el-image {
            height: 180px !important;
        }
    }
}
</style>
